package business;

import constants.Constants;
import models.heimingdan;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import play.Logger;
import play.db.jpa.JPA;
import utils.ErrorInfo;
import utils.PageBean;

import javax.persistence.Query;
import java.math.BigInteger;
import java.util.*;

/**
 * Created by Administrator on 2017-07-18.
 */
public class CommonBiz {
    /**
     *
     * @param currPage
     * @param pageSize
     * @param error
     * @return
     */
    public static PageBean<heimingdan> queryHeimingdanList(int currPage, int pageSize, ErrorInfo error) {
        error.clear();
        if(currPage == 0) {
            currPage = 0;
        }
        if(pageSize == 0) {
            pageSize = Constants.PAGE_SIZE;
        }
        List<Object> values = new ArrayList<Object>();
        PageBean<heimingdan> page = new PageBean<heimingdan>();
        page.currPage = currPage;
        page.pageSize = pageSize;
        List<heimingdan> userDetails = null;
        Logger.info(JSONObject.fromObject(page).toString());
        Query query = JPA.em().createNativeQuery(" select * from heimingdan limit ?,?",heimingdan.class);
        Query queryCount = JPA.em().createNativeQuery("SELECT count(*) from  heimingdan " );
        for(int i=0; i<values.size(); i++){
            query.setParameter(i+1, values.get(i));
        }
        query.setParameter(values.size()+1, currPage);
        query.setParameter(values.size()+2, page.pageSize);
        try {
            userDetails = query.getResultList();
            page.totalCount = ((BigInteger) queryCount.getSingleResult()).intValue();

        } catch (Exception e) {
            e.printStackTrace();
            Logger.error("查询交易记录时："+e.getMessage());

            error.code = -1;
            error.msg = "查询黑名单交易记录失败";
            return null;
        }

        page.page = userDetails;
        error.code = 0;

        return page;
    }
}
